package com.chima.dao;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;



import com.chima.domain.Employees;
import com.chima.domain.JobDetail;
import com.chima.domain.Performance;
import com.chima.domain.Rate;


public class PerformanceDao extends HibernateDaoSupport {
	//分页查询
	public List<Performance>findByCriteria(DetachedCriteria criteria, int firstResult,int maxResults) {
		List<Performance> p=this.getHibernateTemplate().findByCriteria(criteria,firstResult,maxResults);
		return p;
	}
   //计算页面总页数
	public int findByTotalCount(String date ,int dept) {
		 long temp = 547733123132L;
		if(date!=null || !"".equals(date) && dept!=0){
			List list= this.getHibernateTemplate().find("select count(*) from Performance where department.id= '"+dept+"' and date='"+date+"'");
		    temp = (Long) list.get(0);
		}
		
		return (int) temp;
	}
	// 查找页面总页数
	public int findTotalCount(String date ,int dept) {
		long temp = 547738888775806L;
		if(date==null || "".equals(date) && dept==0){
			 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM"); 
	         Calendar   calendar=Calendar.getInstance(); 
	         calendar.setTime(new Date());
	         calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)-1); 
	         date= sdf.format(calendar.getTime());
	         List list= this.getHibernateTemplate().find("select count(*) from Performance  where date='"+date+"'");
	         temp = (Long) list.get(0);
		}
		if(date!=null && dept!=0){
			List list= this.getHibernateTemplate().find("select count(*) from Performance  where department.id= '"+dept+"' and date='"+date+"'");
		    temp = (Long) list.get(0);
		}
		if(date!=null && dept==0){
			List list= this.getHibernateTemplate().find("select count(*) from Performance  where date='"+date+"'");
		    temp = (Long) list.get(0);
		}

		return (int) temp;
	}
	public void update(Performance performance) {
		this.getHibernateTemplate().update(performance);
	}
	public void save(Performance performance) {
		this.getHibernateTemplate().save(performance);
	}
//	public List<Performance> findByDateAndDepart(String date, int depart) {
//		String hql="select * from t_performance t where Month(t.date) = Month('"+date+"') and YEAR(t.date) = YEAR('"+date+"') and t.departID='"+depart+"'";
//		Session session   =this.getSession();
//		List<Performance> p = session.createSQLQuery(hql).addEntity(Performance.class).list();
//		super.releaseSession(session);
//		return p;
//	}
	public List<Performance> findByDateAndDepartment(String date, int depart) {
		String hql=" from Performance t where  t.department.id= ? and t.date= ?";
		Session session   =this.getSession();
		List<Performance> p = session.createQuery(hql).setParameter(0, depart).setParameter(1, date).list();
		super.releaseSession(session);
		return p;
	}
	
//	public Double findMaxTotalCount(String date, int depart) {
//		String hql="select max(totalCount) from t_performance t where Month(t.date) = Month('"+date+"') and YEAR(t.date) = YEAR('"+date+"')and t.departID='"+depart+"'";
//		Session session   =this.getSession();
//		Double  p = (Double) session.createSQLQuery(hql).uniqueResult();
//		super.releaseSession(session);
//		return p;
//	}
//	public Double findMinTotalCount(String date,int depart) {
//		String hql="select Min(totalCount) from t_performance t where Month(t.date) = Month('"+date+"') and YEAR(t.date) = YEAR('"+date+"')and t.departID='"+depart+"'";
//		Session session   =this.getSession();
//		Double  p = (Double) this.getSession().createSQLQuery(hql).uniqueResult();
//		super.releaseSession(session);
//		return p;
//	}
//	public void updateFEN(int chengxu ,Double JINJIZHYAO,Double JINJIBUZHYAO,Double BUJINZHYAO ,Double BUJINBUZHYAO,Double CHUANGXIN,Double totalCount,Employees employee,int yiji,int erji,int sanji, int siji) {
//		String hql=" update Performance p set p.chengxu=?,p.jinjizhongyao=?,p.jinjibuzhongyao=?,p.bujinjizhongyao=?,p.bujinjibuzhongyao=? ,p.chuangxin=? ,p.totalCount=? ,p.yiji=?, p.erji=? ,p.sanji=? ,p.siji=? where p.employee=? ";
//		Session session=this.getSession();
//		session.createQuery(hql).setParameter(0,chengxu).setParameter(1,JINJIZHYAO).setParameter(2, JINJIBUZHYAO).	setParameter(3, BUJINZHYAO).setParameter(4,BUJINBUZHYAO).setParameter(5,CHUANGXIN).setParameter(6, totalCount).setParameter(7, yiji).setParameter(8, erji).setParameter(9, sanji).setParameter(10, siji).setParameter(11, employee).executeUpdate();
//		super.releaseSession(session);
//	}
	public void updateFEN(int chengxu ,Double JINJIZHYAO,Double JINJIBUZHYAO,Double BUJINZHYAO ,Double BUJINBUZHYAO,Double totalCount,Employees employee,int yiji,int erji,int sanji, int siji) {
		String hql=" update Performance p set p.chengxu=?,p.jinjizhongyao=?,p.jinjibuzhongyao=?,p.bujinjizhongyao=?,p.bujinjibuzhongyao=? ,p.totalCount=? ,p.yiji=?, p.erji=? ,p.sanji=? ,p.siji=? where p.employee=? ";
		Session session=this.getSession();
		session.createQuery(hql).setParameter(0,chengxu).setParameter(1,JINJIZHYAO).setParameter(2, JINJIBUZHYAO).	setParameter(3, BUJINZHYAO).setParameter(4,BUJINBUZHYAO).setParameter(5, totalCount).setParameter(6, yiji).setParameter(7, erji).setParameter(8, sanji).setParameter(9, siji).setParameter(10, employee).executeUpdate();
		super.releaseSession(session);
	}
	public void updateTotalTime(Double totaltime ,Employees  employee) {
		String hql=" update Performance p set p.totaltTime=? where p.employee=?";
		Session session=this.getSession();
		session.createQuery(hql).setParameter(0,totaltime ).setParameter(1,employee).executeUpdate();
		super.releaseSession(session);
	}
	public List<Rate> findRateByDateAndDepart(String date, int depart) {
		String hql="select * from t_rate t where t.date='"+date+"' and t.deID='"+depart+"'";
		Session session   =this.getSession();
		List<Rate> r = session.createSQLQuery(hql).addEntity(Rate.class).list();
		super.releaseSession(session);
		return r;
	}
	public void updateTotalTime(Double totaltime ,int  id) {
		String hql=" update Performance p set p.totaltTime=? where p.id=?";
		Session session=this.getSession();
		session.createQuery(hql).setParameter(0,totaltime ).setParameter(1,id).executeUpdate();
		super.releaseSession(session);
	}
	public Performance findById(int id) {
		String hql=" from Performance t where  t.id= ? ";
		Session session   =this.getSession();
		Performance p = (Performance) session.createQuery(hql).setParameter(0,id).uniqueResult();
		super.releaseSession(session);
		return p;
	}
	public Performance findByDateAndEmp(String date, int emp) {
		String hql=" from Performance t where  t.employee.id= ? and t.date= ?";
		Session session   =this.getSession();
		Performance p = (Performance) session.createQuery(hql).setParameter(0, emp).setParameter(1, date).uniqueResult();
		super.releaseSession(session);
		return p;
	}
	public void DeletePerformance(int empid) {
		
//		String hql=" DELETE * FROM Performance r where r.employee.id=?";
//		Session session=this.getSession();
//		session.createQuery(hql).setParameter(0,empid ).executeUpdate();
//		super.releaseSession(session);
		
		 Session session=this.getSession();
		 String sql1=" delete  from t_performance where empID=?";
		 session.createSQLQuery(sql1).setParameter(0, empid).executeUpdate();
		 super.releaseSession(session);
	}
	
	public List<Performance> findByDate(String date) {
		String hql=" from Performance t where  t.date= ?";
		Session session   =this.getSession();
		List<Performance> p = session.createQuery(hql).setParameter(0, date).list();
		super.releaseSession(session);
		return p;
	}
	public List<Performance> findallEmpPerformanceBydate(String searchdate) {
		String hql=" from Performance t where  t.date= ?";
		Session session   =this.getSession();
		List<Performance> p = session.createQuery(hql).setParameter(0, searchdate).list();
		super.releaseSession(session);
		return p;
	}
	
}
